package com.ericsson.entironment.manager;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.common.db.DatabaseMng;
import com.ericsson.common.exception.SmafException;
import com.ericsson.entironment.util.ResultCode;

public class ContactManager
{
	private static Log log =LogFactory.getLog(ContactManager.class);
	public static ResultCode addNewContect(String name,String loginname,String password,String tel)
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		String optrid;
		ResultCode code=new ResultCode();
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			//检查用户名是否重复
			sql="select * from ema_operator where optrname='"+name+"' or loginname='"+loginname+"'";
			rst=stmt.executeQuery(sql);
			if (rst.next())
			{//操作员重名
				code.setResult("failure");
				code.setMessage("用户名或登录名重复");
				return code;
			}
			//加入操作员
			optrid=UUID.randomUUID().toString();
			sql="insert into ema_operator(optrid,optrname,loginname,optrpasswd,optrtel) values ('"
				+optrid+"','"+name+"','"+loginname+"','"+password+"','"+tel+"')";

			stmt.executeUpdate(sql);
			code.setResult("success");
		}
		catch (Exception e)
		{
			log.error("新增操作员列表失败 sql["+sql+"]",e);
			code.setResult("failure");
			code.setMessage("系统错误，无法完成操作员新增");
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		
		return code;
	}
	
	public static List getContectList()
	{
		List list=new ArrayList();
		List contact=null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			sql="select * from ema_operator";
			rst=stmt.executeQuery(sql);
			while (rst.next())
			{
				contact=new ArrayList();
				contact.add(rst.getString("optrid"));
				contact.add(rst.getString("optrname"));
				contact.add(rst.getString("optrtel"));
				contact.add("<a href='javascript:delContect(\""+rst.getString("optrid")+"\",\""+rst.getString("optrname")+"\")'><img src='./image/delete.gif' width=16 height=16 title='删除记录'></a>");
				list.add(contact);
			}
		}
		catch (Exception e)
		{
			log.error("获取操作员列表失败 sql["+sql+"]",e);
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		
		return list;
	}
	public static void del(String id) throws SmafException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			sql="delete from ema_operator where optrid='"+id+"'";

			stmt.executeUpdate(sql);
		}
		catch (Exception e)
		{
			log.error("新增操作员列表失败 sql["+sql+"]",e);
			throw new SmafException("删除操作员失败");
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}
}
